<template>
  <!-- <el-dialog title="请填写个人信息" :visible.sync="dialogFormVisible"> -->
  <el-form ref="fromRef" :model="form" :rules="rules">
    <el-form-item label="账号" prop="id" :label-width="formLabelWidth">
      <el-input v-model="form.id" autocomplete="off" />
    </el-form-item>
    <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
      <el-input v-model="form.password" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
      <el-input v-model="form.email" autocomplete="off" />
    </el-form-item>
    <el-form-item label="手机号" prop="mobile" :label-width="formLabelWidth">
      <el-input v-model="form.mobile" autocomplete="off" />
    </el-form-item>
    <el-form-item label="昵称" prop="username" :label-width="formLabelWidth">
      <el-input v-model="form.username" autocomplete="off" maxlength="8" />
    </el-form-item>
    <el-form-item label="生日" prop="birthday" :label-width="formLabelWidth">
      <el-date-picker
        v-model="form.birthday"
        type="date"
        placeholder="选择日期"
        format="yyyy 年 MM 月 dd 日"
        value-format="timestamp"
        style="width: 100%"
        @change="pickerChange"
      /></el-form-item>
    <el-form-item label="年龄" prop="age" :label-width="formLabelWidth">
      <el-input v-model="form.age" autocomplete="off" />
    </el-form-item>
    <div class="dialog-footer">
      <el-button @click="cancelBtn">取 消</el-button>
      <el-button
        type="primary"
        @click="confimBtn"
      >确 定</el-button>
    </div>
  </el-form>

  <!-- </el-dialog> -->
</template>

<script>
export default {
  name: 'Register',
  data() {
    // 账号自定义校验
    const validateId = (rule, value, callback) => {
      const reg = /^[a-zA-Z0-9]{8,116}$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('账号为8-16位,包含大小写字母和数字'))
      }
    }
    // 密码自定义校验
    const validatePass = (rule, value, callback) => {
      const reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,16}$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('密码为6-16位必须同时包含大小写字母数字和特殊字符'))
      }
    }
    // 邮箱的自定义校验
    const validateEmail = (rule, value, callback) => {
      const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('邮箱格式不正确'))
      }
    }
    // 手机号自定义校验
    const validateMobile = (rule, value, callback) => {
      const reg =
        /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('手机号码格式不正确'))
      }
    }
    return {
      isShow: false,
      oldData: '',
      form: {
        id: '',
        password: '',
        email: '',
        mobile: '',
        username: '',
        birthday: '',
        age: ''
      },
      rules: {
        id: [
          { required: true, message: '账号名不能为空', trigger: 'blur' },
          { validator: validateId, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱地址不能为空', trigger: 'blur' },
          {
            validator: validateEmail,
            trigger: ['blur', 'change']
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        username: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ],
        birthday: [{ required: true, message: '必须选择', trigger: 'blur' }]
      },
      formLabelWidth: '80px'
    }
  },
  methods: {
    pickerChange(value) {
      this.oldData = value
      const newData = +new Date()
      this.form.age = parseInt(
        (newData - this.oldData) / 1000 / 60 / 60 / 24 / 365
      )
    },
    confimBtn() {
      this.$refs.fromRef.validate(isOk => {
        if (isOk) {
        // 点击确定校验通过的话通知父组件关闭弹层
          console.log(this.form)
          this.$emit('closeDialog') // 点击取消通知父组件关闭弹层重置表单
        }
      })
    },
    cancelBtn() {
    //   this.$refs.fromRef.resetFields()
      this.$emit('closeDialog') // 点击取消通知父组件关闭弹层重置表单
    }
  }
}
</script>

<style>
</style>
